﻿/*******************************************************************************
#                    ExpertBelongDomainBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-8-29 22:14:47
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
#endregion

namespace ExpertMIS.Brokers
{
    public class ExpertBelongDomainBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateExpertBelongDomainEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateExpertBelongDomainEntity", entity);
        }

        private object InsertExpertBelongDomainEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertExpertBelongDomainEntity", entity);
        }

        private object DeleteExpertBelongDomainEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteExpertBelongDomainEntity", entity);
        }

        private void RemoveDomainByExpertOID(string ExpertOID)
        {
            SqlMap.Delete("DeleteExpertBelongDomainsByExpertOID", ExpertOID);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectExpertBelongDomainEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            if (entity.EntityState == EntityState.New)
            {
                InsertExpertBelongDomainEntity(entity);
                entity.EntityState = EntityState.Old;
            }
            else
            {
                UpdateExpertBelongDomainEntity(entity);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            ExpertBelongDomainEntity entity = new ExpertBelongDomainEntity();

            // initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteExpertBelongDomainEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Method

        public IList<ExpertBelongDomainEntity> GetDomainByExpertOID(string ExpertOID)
        {
            return SqlMap.QueryForList<ExpertBelongDomainEntity>("SelectExpertBelongDomainEntityByExpertOID", ExpertOID);
        }

        public void UpdateExpertDomains(string ExpertOID, List<ExpertBelongDomainEntity> list)
        {
            try
            {
                SqlMap.BeginTransaction();

                RemoveDomainByExpertOID(ExpertOID);

                foreach (ExpertBelongDomainEntity entity in list)
                {
                    InsertExpertBelongDomainEntity(entity);
                }

                SqlMap.CommitTransaction();
            }
            catch
            {
                SqlMap.RollBackTransaction();
            }
        }

        #endregion
    }
}